package com.tiger.web.controller.communal;

import java.util.List;

import com.tiger.common.core.domain.AjaxMapResult;
import com.tiger.common.core.domain.model.LoginUser;
import com.tiger.common.utils.ServletUtils;
import com.tiger.common.utils.spring.SpringUtils;
import com.tiger.communal.domain.ConstructionUnit;
import com.tiger.communal.service.ConstructionUnitOa;
import com.tiger.communal.service.IConstructionUnitService;
import com.tiger.framework.web.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tiger.common.annotation.Log;
import com.tiger.common.core.controller.BaseController;
import com.tiger.common.core.domain.AjaxResult;
import com.tiger.common.enums.BusinessType;
import com.tiger.common.core.page.TableDataInfo;

/**
 * 【施工单位管理】Controller
 *
 * @author tiger
 * @date 2022-09-13
 */
@RestController
@RequestMapping("/system/unit")
@Api(tags = "施工单位管理")
public class ConstructionUnitController extends BaseController {
    @Autowired
    private IConstructionUnitService constructionUnitService;

    /**
     * 查询【施工单位管理】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:unit:list')")
    @ApiOperation(value = "查询【施工单位管理】列表")
    @GetMapping("/list")
    public TableDataInfo list(ConstructionUnit constructionUnit) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        constructionUnit.setCompanyId(loginUser.getUser().getDeptId());
        startPage();
        List<ConstructionUnit> list = constructionUnitService.selectConstructionUnitList(constructionUnit);
        return getDataTable(list);
    }

    /**
     * 获取【施工单位管理】详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:unit:query')")
    @ApiOperation(value = "查询【施工单位管理】列详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(constructionUnitService.selectConstructionUnitById(id));
    }

    /**
     * 新增【施工单位管理】
     */
//    @PreAuthorize("@ss.hasPermi('system:unit:add')")
//    @Log(title = "【施工单位管理】", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增【施工单位管理】")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ConstructionUnit constructionUnit) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        constructionUnit.setCompanyId(loginUser.getUser().getDeptId());
        return toAjax(constructionUnitService.insertConstructionUnit(constructionUnit));
    }

    /**
     * 修改【施工单位管理】
     */
//    @PreAuthorize("@ss.hasPermi('system:unit:edit')")
//    @Log(title = "【施工单位管理】", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改【施工单位管理】")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody ConstructionUnit constructionUnit) {
        return toAjax(constructionUnitService.updateConstructionUnit(constructionUnit));
    }

    /**
     * 删除【施工单位管理】
     */
//    @PreAuthorize("@ss.hasPermi('system:unit:remove')")
//    @Log(title = "【施工单位管理】", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除【施工单位管理】")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(constructionUnitService.deleteConstructionUnitByIds(ids));
    }

    @ApiOperation(value = "施工单位管理审核回调")
    @GetMapping("/callBack")
    public AjaxResult callBack(@ApiParam(value = "施工单位id") Long id,
                               @ApiParam(value = "审核状态 3-审批通过，4-已驳回") Long status,String oaFile) {
        return toAjax(constructionUnitService.callBack(id, status,oaFile));
    }


    @Autowired
    private ConstructionUnitOa constructionUnitOa;
    /**
     * 施工单位提交审批
     */
    @ApiOperation(value = "施工单位提交审批")
    @PostMapping("/oaConstructionUnit")
    public AjaxMapResult oaConstructionUnit(@RequestBody ConstructionUnit constructionUnit) throws Exception {
        return AjaxMapResult.success(constructionUnitOa.oaConstructionUnit(constructionUnit));
    }
}
